var cfg = require('../../utils/cfg');
var util = require('../../utils/util');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchant: {}

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadMerchant();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  loadMerchant: function () {
    let that = this;
    sendRequest.sendReq(cfg.djurl.loadMerchant, {
      openId: 'xxx'
    }, function (result) {
      that.setData({
        merchant: result.data
      });
    });
  },

  changeStatus : function(flag) {
    let that = this;

    sendRequest.sendReq(cfg.djurl.changeMerchantStatus, {
      openId: 'xxx',
      status: flag ? 2 : 3
    }, function (result) {
    }, function () { }, function () {
      that.setData({
        merchant: that.data.merchant
      });
    });
  },

  changeStatusHandler : function(event) {
    let that = this;

    if (!event.detail.value) {
      wx.showModal({
        title: '提示',
        content: '确定要打烊吗?',
        success(res) {
          if (res.cancel) {
            console.log('用户点击取消')
            that.setData({
              merchant: that.data.merchant
            });
            return;
          } else {
            that.changeStatus(false);
          }
        }
      })
    } else {
      that.changeStatus(true);
    }

  }
})